package cn.liyunqi.ttv.start.controller;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.StringUtil;
import cn.liyunqi.ttv.start.action.TTVActionUtil;
import cn.liyunqi.ttv.start.localdb.ConfigHelper;
import cn.liyunqi.ttv.start.util.CommonUtil;
import cn.liyunqi.ttv.start.view.AlertUtil;
import com.alibaba.fastjson.JSONObject;
import javafx.application.Platform;
import javafx.fxml.FXML;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.scene.control.TextField;

public class LoginController extends BaseFXController {

    @FXML
    private TextField mailTxt;

    @FXML
    private TextField passwordTxt;


    private InitAppController initUIController;

    public void setInitUIController(InitAppController initUIController) {
        this.initUIController = initUIController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            String mail = ConfigHelper.getloadValue(ConfigHelper.LOGIN_ID);

            String password = ConfigHelper.getloadValue(ConfigHelper.PASSWORD);

            if(!StringUtil.isEmptyOrNull(mail)){
                mailTxt.setText(mail);
            }
            if(!StringUtil.isEmptyOrNull(password)){
                passwordTxt.setText(password);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void doLoginHandle(){

        /*if(true){
            CommonUtil.wakeUpMain("184dbe3fd7434e1591b8f1c6ed5580e4","kiss598652881@sina.com","a16593bbcb094affa4538c6b59488807",new Long("1529741723232"));
            this.initUIController.getPrimaryStage().close();
            this.getDialogStage().close();
            return;
        }
        */
        String mailValue = mailTxt.getText();
        if(StringUtil.isEmptyOrNull(mailValue) || !TTVActionUtil.isEmail(mailValue)){
            AlertUtil.showErrorAlert("登录邮箱不能为空");
            return;
        }
        String password = passwordTxt.getText();
        if(password == null || password.isEmpty()){
            AlertUtil.showErrorAlert("登录密码不能为空");
            return;
        }
        try {

            JSONObject user = TTVActionUtil.login(mailValue, password);

            if(user == null || StringUtil.isEmptyOrNull(user.getString("id"))){
                throw new BizException("登录失败");
            }
            //存储账号密码
            ConfigHelper.updateValueByName(ConfigHelper.LOGIN_ID,mailValue);
            ConfigHelper.updateValueByName(ConfigHelper.PASSWORD,password);
            ConfigHelper.updateValueByName("userId",user.getString("id"));

            //存储本地userId token
            ConfigHelper.insertOrUpdateUserInfoTokenByUserId(user.getString("id"),user.getString("token"));

            //调用第二个程序
            CommonUtil.wakeUpMain(user.getString("id"),mailValue,user.getString("token"),user.getLongValue("limitTime"));

            this.initUIController.getPrimaryStage().close();
            this.getDialogStage().close();


        }catch (BizException ex){
            AlertUtil.showErrorAlert(ex.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            AlertUtil.showErrorAlert("登录失败");
        }
    }

    @FXML
    public void forgetPasswordHandle(){
        ForgetPasswordController controller = (ForgetPasswordController) loadFXMLPage("找回密码", FXMLPage.ForgetPassword, false);
        controller.setInitUIController(this.initUIController);
        controller.showDialogStage();
    }
}
